package com.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
public class FileUtil {
    public static void deleteFile(String path) {
        if (path != null && path.length() > 0) {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
        }
    }
    public static void deleteAttFile(File file, String path) {
        if (!StringUtil.isEmpty(path)) {
            File f = new File(file, path);
            if (f.exists()) {
                f.delete();
            }
            File initFile = new File(file, "init/" + path);
            if (initFile.exists()) {
                initFile.delete();
            }
        }
    }
    public static boolean write(File file, String content) {
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            FileWriter fw = new FileWriter(file);
            BufferedWriter write = new BufferedWriter(fw);
            write.write(content);
            write.close();
            fw.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void writeInBinary(File file,InputStream in) throws Exception {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            byte b[] = new byte[1024*1024*3];
            int j = 0;
            while ((j = in.read(b)) != -1) {
                out.write(b, 0, j);
            }
            out.flush();
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }
    public static boolean write(File file, String content,String charset) {
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            OutputStream fw = new FileOutputStream(file);
            OutputStreamWriter write = new OutputStreamWriter(fw,charset);
            write.write(content);
            write.close();
            fw.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }





    public static String read(File file) {
        if (!file.exists()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Reader fis = null;
        try {
            fis = new InputStreamReader(new FileInputStream(file));
            char[] buf = new char[4 * 1024]; // 4K
            int n;
            while ((n = fis.read(buf)) != -1) {
                sb.append(buf, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return sb.toString();
    }
    public static String read(File file,String charset) {
        if (!file.exists()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Reader fis = null;
        try {
            fis = new InputStreamReader(new FileInputStream(file),charset);
            char[] buf = new char[4 * 1024]; // 4K
            int n;
            while ((n = fis.read(buf)) != -1) {
                sb.append(buf, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return sb.toString();
    }

    public static String getExtension(String name) {
        int i = name.lastIndexOf('.');
        if (i >= 0) {
            return name.substring(i + 1).toLowerCase();
        }
        return "";
    }
    public static String getFileName(String path) {
        try {
            char separatorChar = '/';
            int index = path.lastIndexOf(separatorChar);
            if (index < 0) {
                separatorChar = '\\';
                index = path.lastIndexOf(separatorChar);
            }
            return path.substring(index + 1);
        } catch (Exception ex) {
            return "Unknown";
        }
    }
    public static SimpleDateFormat DIR_PATH_DATA_FORMAT = new SimpleDateFormat("yyyy/MM/dd/");
    private static int currTime = (int) (System.currentTimeMillis() / 1000);
    private static synchronized long getNext() {
        return currTime++;
    }
    public static String getUniqueFileName() {
        return DIR_PATH_DATA_FORMAT.format(new Date()) + getNext();
    }
}